using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;

namespace HIS
{
    public class HistoryQueryForm : Form
    {
        private TextBox txtPatientInfo;
        private TabControl tabControl;
        private TabPage tabFee;
        private TabPage tabDiagnosis;
        private TabPage tabOrder;
        private TabPage tabHistory;
        private TabPage tabTreatment;
        private TabPage tabAllergy;
        private TabPage tabTest;

        public HistoryQueryForm()
        {
            InitializeComponents();
        }

        private void InitializeComponents()
        {
            // 窗体设置
            this.Text = "历史信息查询";
            this.Size = new Size(1000, 700);
            this.StartPosition = FormStartPosition.CenterScreen;

            // 病人信息查询区域
            GroupBox gbPatientInfo = new GroupBox
            {
                Text = "病人信息查询",
                Location = new Point(10, 10),
                Size = new Size(980, 60)
            };

            Label lblPatientInfo = new Label
            {
                Text = "病人信息：",
                Location = new Point(10, 25),
                AutoSize = true
            };

            txtPatientInfo = new TextBox
            {
                Location = new Point(90, 22),
                Size = new Size(300, 25)
            };

            Button btnQuery = new Button
            {
                Text = "查询",
                Location = new Point(400, 22),
                Size = new Size(80, 25)
            };

            gbPatientInfo.Controls.AddRange(new Control[] {
                lblPatientInfo,
                txtPatientInfo,
                btnQuery
            });

            // 创建选项卡控件
            tabControl = new TabControl
            {
                Location = new Point(10, 80),
                Size = new Size(980, 600)
            };

            // 费用信息选项卡
            tabFee = new TabPage("费用信息");
            InitializeFeeTab();

            // 历史诊断选项卡
            tabDiagnosis = new TabPage("历史诊断");
            InitializeDiagnosisTab();

            // 就诊医嘱选项卡
            tabOrder = new TabPage("就诊医嘱");
            InitializeOrderTab();

            // 既往史选项卡
            tabHistory = new TabPage("既往史");
            InitializeHistoryTab();

            // 治疗过程选项卡
            tabTreatment = new TabPage("治疗过程");
            InitializeTreatmentTab();

            // 药物过敏史选项卡
            tabAllergy = new TabPage("药物过敏史");
            InitializeAllergyTab();

            // 检验检查结果选项卡
            tabTest = new TabPage("检验检查结果");
            InitializeTestTab();

            // 添加选项卡
            tabControl.TabPages.AddRange(new TabPage[] {
                tabFee,
                tabDiagnosis,
                tabOrder,
                tabHistory,
                tabTreatment,
                tabAllergy,
                tabTest
            });

            // 添加控件到窗体
            this.Controls.AddRange(new Control[] {
                gbPatientInfo,
                tabControl
            });

            // 修改查询按钮事件
            btnQuery.Click += (s, e) =>
            {
                QueryPatientHistory();
            };
        }

        private void QueryPatientHistory()
        {
            string patientInfo = txtPatientInfo.Text.Trim();
            if (string.IsNullOrEmpty(patientInfo))
            {
                MessageBox.Show("请输入病人信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // TODO: 从数据库查询病人历史信息
            // 示例代码：
            // var patientHistory = Database.GetPatientHistory(patientInfo);
            // LoadFeeData(patientHistory.FeeRecords);
            // LoadDiagnosisData(patientHistory.DiagnosisRecords);
            // LoadOrderData(patientHistory.OrderRecords);
            // LoadHistoryData(patientHistory.HistoryRecords);
            // LoadTreatmentData(patientHistory.TreatmentRecords);
            // LoadAllergyData(patientHistory.AllergyRecords);
            // LoadTestData(patientHistory.TestRecords);
        }

        private void LoadFeeData(DataTable feeData)
        {
            var dgvFee = tabFee.Controls[0] as DataGridView;
            dgvFee.Rows.Clear();
            foreach (DataRow row in feeData.Rows)
            {
                dgvFee.Rows.Add(
                    row["日期"],
                    row["费用类型"],
                    row["金额"],
                    row["支付方式"],
                    row["状态"]
                );
            }
        }

        private void LoadDiagnosisData(DataTable diagnosisData)
        {
            var dgvDiagnosis = tabDiagnosis.Controls[0] as DataGridView;
            dgvDiagnosis.Rows.Clear();
            foreach (DataRow row in diagnosisData.Rows)
            {
                dgvDiagnosis.Rows.Add(
                    row["日期"],
                    row["诊断名称"],
                    row["诊断类型"],
                    row["医生"]
                );
            }
        }

        private void LoadOrderData(DataTable orderData)
        {
            var dgvOrder = tabOrder.Controls[0] as DataGridView;
            dgvOrder.Rows.Clear();
            foreach (DataRow row in orderData.Rows)
            {
                dgvOrder.Rows.Add(
                    row["日期"],
                    row["医嘱类型"],
                    row["医嘱内容"],
                    row["医生"]
                );
            }
        }

        private void LoadHistoryData(DataTable historyData)
        {
            var txtHistory = tabHistory.Controls[0] as TextBox;
            txtHistory.Text = historyData.Rows[0]["既往史内容"].ToString();
        }

        private void LoadTreatmentData(DataTable treatmentData)
        {
            var dgvTreatment = tabTreatment.Controls[0] as DataGridView;
            dgvTreatment.Rows.Clear();
            foreach (DataRow row in treatmentData.Rows)
            {
                dgvTreatment.Rows.Add(
                    row["日期"],
                    row["治疗项目"],
                    row["治疗结果"],
                    row["医生"]
                );
            }
        }

        private void LoadAllergyData(DataTable allergyData)
        {
            var dgvAllergy = tabAllergy.Controls[0] as DataGridView;
            dgvAllergy.Rows.Clear();
            foreach (DataRow row in allergyData.Rows)
            {
                dgvAllergy.Rows.Add(
                    row["药物名称"],
                    row["过敏反应"],
                    row["记录时间"]
                );
            }
        }

        private void LoadTestData(DataTable testData)
        {
            var dgvTest = tabTest.Controls[0] as DataGridView;
            dgvTest.Rows.Clear();
            foreach (DataRow row in testData.Rows)
            {
                dgvTest.Rows.Add(
                    row["日期"],
                    row["检验项目"],
                    row["检验结果"],
                    row["参考范围"],
                    row["单位"]
                );
            }
        }

        private void InitializeFeeTab()
        {
            DataGridView dgvFee = new DataGridView
            {
                Location = new Point(10, 10),
                Size = new Size(960, 550),
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect
            };

            dgvFee.Columns.AddRange(new DataGridViewColumn[] {
                new DataGridViewTextBoxColumn { Name = "日期", HeaderText = "日期", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "费用类型", HeaderText = "费用类型", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "金额", HeaderText = "金额", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "支付方式", HeaderText = "支付方式", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "状态", HeaderText = "状态", Width = 100 }
            });

            tabFee.Controls.Add(dgvFee);
        }

        private void InitializeDiagnosisTab()
        {
            DataGridView dgvDiagnosis = new DataGridView
            {
                Location = new Point(10, 10),
                Size = new Size(960, 550),
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect
            };

            dgvDiagnosis.Columns.AddRange(new DataGridViewColumn[] {
                new DataGridViewTextBoxColumn { Name = "日期", HeaderText = "日期", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "诊断名称", HeaderText = "诊断名称", Width = 200 },
                new DataGridViewTextBoxColumn { Name = "诊断类型", HeaderText = "诊断类型", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "医生", HeaderText = "医生", Width = 100 }
            });

            tabDiagnosis.Controls.Add(dgvDiagnosis);
        }

        private void InitializeOrderTab()
        {
            DataGridView dgvOrder = new DataGridView
            {
                Location = new Point(10, 10),
                Size = new Size(960, 550),
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect
            };

            dgvOrder.Columns.AddRange(new DataGridViewColumn[] {
                new DataGridViewTextBoxColumn { Name = "日期", HeaderText = "日期", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "医嘱类型", HeaderText = "医嘱类型", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "医嘱内容", HeaderText = "医嘱内容", Width = 300 },
                new DataGridViewTextBoxColumn { Name = "医生", HeaderText = "医生", Width = 100 }
            });

            tabOrder.Controls.Add(dgvOrder);
        }

        private void InitializeHistoryTab()
        {
            TextBox txtHistory = new TextBox
            {
                Location = new Point(10, 10),
                Size = new Size(960, 550),
                Multiline = true,
                ReadOnly = true
            };

            tabHistory.Controls.Add(txtHistory);
        }

        private void InitializeTreatmentTab()
        {
            DataGridView dgvTreatment = new DataGridView
            {
                Location = new Point(10, 10),
                Size = new Size(960, 550),
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect
            };

            dgvTreatment.Columns.AddRange(new DataGridViewColumn[] {
                new DataGridViewTextBoxColumn { Name = "日期", HeaderText = "日期", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "治疗项目", HeaderText = "治疗项目", Width = 200 },
                new DataGridViewTextBoxColumn { Name = "治疗结果", HeaderText = "治疗结果", Width = 300 },
                new DataGridViewTextBoxColumn { Name = "医生", HeaderText = "医生", Width = 100 }
            });

            tabTreatment.Controls.Add(dgvTreatment);
        }

        private void InitializeAllergyTab()
        {
            DataGridView dgvAllergy = new DataGridView
            {
                Location = new Point(10, 10),
                Size = new Size(960, 550),
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect
            };

            dgvAllergy.Columns.AddRange(new DataGridViewColumn[] {
                new DataGridViewTextBoxColumn { Name = "药物名称", HeaderText = "药物名称", Width = 200 },
                new DataGridViewTextBoxColumn { Name = "过敏反应", HeaderText = "过敏反应", Width = 300 },
                new DataGridViewTextBoxColumn { Name = "记录时间", HeaderText = "记录时间", Width = 150 }
            });

            tabAllergy.Controls.Add(dgvAllergy);
        }

        private void InitializeTestTab()
        {
            DataGridView dgvTest = new DataGridView
            {
                Location = new Point(10, 10),
                Size = new Size(960, 550),
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                ReadOnly = true,
                SelectionMode = DataGridViewSelectionMode.FullRowSelect
            };

            dgvTest.Columns.AddRange(new DataGridViewColumn[] {
                new DataGridViewTextBoxColumn { Name = "日期", HeaderText = "日期", Width = 100 },
                new DataGridViewTextBoxColumn { Name = "检查项目", HeaderText = "检查项目", Width = 200 },
                new DataGridViewTextBoxColumn { Name = "检查结果", HeaderText = "检查结果", Width = 300 },
                new DataGridViewTextBoxColumn { Name = "医生", HeaderText = "医生", Width = 100 }
            });

            tabTest.Controls.Add(dgvTest);
        }
    }
} 